﻿using System.Collections.Generic;
using System.Linq;
using EBE.COM.Core.Interfaces;
using EBE.COM.Core.Models;
using Moq;
using NUnit.Framework;

namespace EBE.COM.Core.Test.Categories
{
  [TestFixture]
  public class TestCategories
  {
    private Mock<ICategoryFactory> _factory;
    private Mock<ICategoryRepository> _repository;
    private const int CatId = 101;

    [SetUp]
    public void Init()
    {
      // factory
      _factory = new Mock<ICategoryFactory>();
      _factory.Setup(e => e.MakeCategories()).Returns(Make10Categories());
      _factory.Setup(e => e.MakeCategory(CatId)).Returns(new Category { Id = CatId, Name = "category name" });
      // repository
      _repository = new Mock<ICategoryRepository>();
      _repository.Setup(e => e.FindAll()).Returns(_factory.Object.MakeCategories());
      _repository.Setup(e => e.FindById(CatId)).Returns(_factory.Object.MakeCategory(CatId));
    }

    [Test]
    public void Test_I_Have_Got_10_Categories_In_My_Repository()
    {
      var categories = _repository.Object.FindAll();
      Assert.AreEqual(10, categories.Count());
    }

    [Test]
    public void Test_A_Category_Should_Have_An_Id_Greater_Than_Zero()
    {
      var category = _repository.Object.FindById(CatId);
      Assert.IsTrue(category.Id > 0);
    }

    private static IQueryable<ICategory> Make10Categories()
    {
      var list = new List<ICategory>();
      for (var i = 0; i < 10; i++)
      {
        list.Add(new Category());
      }
      return list.AsQueryable();
    }

    [Test]
    public void Test_A_Category_Needs_A_Name()
    {
      var category = _repository.Object.FindById(101);
      Assert.IsTrue(category.Name.Length > 0);
    }
  }
}
